package GUI;
/**
 * Principle user type main menu
 */
import java.awt.Dimension;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;


import Common.*;


public class PrincipleMainMenu extends JFrame {
	private JMenuBar menuBar=null;
	private JMenu mStatisticalReports=null;
	private JMenuItem mIStatTeachersClass=null;
	private JMenuItem mICoursesforClass=null;
	private JMenuItem mIClassesSameTeacher=null;
	
	private JMenu mPersonalData=null;
	private JMenuItem mntmViewPersonalData=null;
	private JMenu mUserCntrl=null;
	private JMenuItem mntmBlockParentalAccess=null;
	private JMenuItem mntmUnblockParentalAccess=null;
	private JMenuItem logout = null;
	
	private JMenu mnExceptionalRequests = null;
	private JMenuItem mntmStudentRemovalFrom = null;
	private JMenuItem mntmTeacherAllocationTo = null;
	private JMenuItem mntmStudentAdditionTo = null;
	
	
	private PrincipleVpdSelClass selClassScrn = null;
	private PrincipleVpdSelStudent selStudentScrn = null;
	private PrincipleVpdSelYearnSem selYearNSemScrn = null;
	private PrincipleShowDataScreen showDataScrn = null;
	
	public Login myLoginFrame = null;
	public static Dimension defaultDim=null;
	public UserPrinciple user;
	public PrincipleMainMenu prmn = null;
	
	private StatRepBClasses4Teacher bClasses4TeacherScrn = null;
	private StatRepBCourses4Class bCourses4ClassScrn = null;
	private StatRepBTeachers4Grade bTeachers4ClassScrn = null;
	private Histogram his = null;
	private Histogram4StatBCourses4Class hisC4C = null;
	private Histogram4StatBTeacher4Class hisT4C = null;
	
	
	private PrincipleApprvTeacherAlloc apprvTeacherAllocScreen = null;
	private PrincipleApprvStudentAdd apprvStudentAdd = null;
	private PrincipleApprvStudentRemove apprvStudentRmv = null;
	
	
	public PrincipleApprvTeacherAlloc getApprvTeacherAllocScreen()
	{
		if(apprvTeacherAllocScreen==null)
		{
			apprvTeacherAllocScreen = new PrincipleApprvTeacherAlloc(this);
			apprvTeacherAllocScreen.myLoginFrame = myLoginFrame;
			apprvTeacherAllocScreen.setSize(getMaximumSize());
			apprvTeacherAllocScreen.prmn= this;
			apprvTeacherAllocScreen.user = user;
		}
		return apprvTeacherAllocScreen;
	}
	
	
	
	
	public PrincipleApprvStudentAdd getApprvStudentAdd()
	{
		if(apprvStudentAdd==null)
		{
			apprvStudentAdd = new PrincipleApprvStudentAdd(this);
			apprvStudentAdd.myLoginFrame = myLoginFrame;
			apprvStudentAdd.setSize(getMaximumSize());
			apprvStudentAdd.prmn= this;
			apprvStudentAdd.user = user;
		}
		return apprvStudentAdd;
	}
	
	

	public PrincipleApprvStudentRemove getApprvStudentRmv()
	{
		if(apprvStudentRmv==null)
		{
			apprvStudentRmv = new PrincipleApprvStudentRemove(this);
			apprvStudentRmv.myLoginFrame = myLoginFrame;
			apprvStudentRmv.setSize(getMaximumSize());
			apprvStudentRmv.prmn= this;
			apprvStudentRmv.user = user;
		}
		return apprvStudentRmv;
	}
	
	
	private void initialize(PrincipleMainMenu prmn)
	{
		
		this.setSize(new Dimension(700, 600));
		this.setTitle("Principle main menu");
		this.setJMenuBar(createMenuBar());
		
		
		mStatisticalReports = getMenuStatReports();
		mPersonalData = getMenuPersonalData();
		mntmViewPersonalData = getMenuItemPersonalData();
		mnExceptionalRequests=getMenuExceptionalRequests();
		mntmStudentRemovalFrom=getMenuItemStudentRemovalFromCourse();
		mntmTeacherAllocationTo=getMenuItemTeacherAllocation();
		mntmStudentAdditionTo=getMenuItemStudentAdditionToCourse();
		mIStatTeachersClass=getMenuItemStatTeacherClass();
		mICoursesforClass=getMenuItemStatCoursesForClass();
		mIClassesSameTeacher=getMenuItemStatClassesSameTeacher();
	    logout= getLogoutMi();
		
		menuBar.add(mStatisticalReports);
		menuBar.add(mnExceptionalRequests);
		menuBar.add(mPersonalData);
		menuBar.add(logout);
		
		
		 
			
		
		mnExceptionalRequests.add(mntmStudentRemovalFrom);
		mnExceptionalRequests.add(mntmTeacherAllocationTo);
		mnExceptionalRequests.add(mntmStudentAdditionTo);
		mStatisticalReports.add(mIStatTeachersClass);
		mStatisticalReports.add(mICoursesforClass);
		mStatisticalReports.add(mIClassesSameTeacher);
		
		mPersonalData.add(mntmViewPersonalData);

	}
	
	
	private JMenuBar createMenuBar()
	{
		if (menuBar==null)
		{
			menuBar = new JMenuBar();
			 
		}		
		return menuBar;
	}
	
	
	 public JMenu getMenuExceptionalRequests()
	    {
	    	if(mnExceptionalRequests==null)
	    	{
	    		mnExceptionalRequests = new JMenu("Exceptional requests");
	    	}
	    	return mnExceptionalRequests;
	    }
	 
	  public JMenuItem getMenuItemStudentRemovalFromCourse()
	    {
	    	if(mntmStudentRemovalFrom==null)
	    	{
	    		 mntmStudentRemovalFrom = new JMenuItem("Student removal from course");
	    		 mntmStudentRemovalFrom.addActionListener(new java.awt.event.ActionListener()
	    		{	
	    			public void actionPerformed(java.awt.event.ActionEvent e)
	    		
	    			{
	    				user.dataExRemStudToCourse=null;
	    				myLoginFrame.client.handleMessageFromClientUI("ExReqStudRemQuery");
	    				while(user.dataExRemStudToCourse==null);
	    				System.out.println("actionPerformed");
	    			/*************************************************************************************/	
	    				for(int i=0;i<user.dataExRemStudToCourse.size();i++)
	    				{
	    					System.out.println("Exreq: " +user.dataExRemStudToCourse.get(i).Sid );
	    				}
	    				/*************************************************************************************/		
	    				apprvStudentRmv=null;
	    				apprvStudentRmv = getApprvStudentRmv();
	    				setContentPane(apprvStudentRmv);
	    				
	    				
	    				//TODO what happends when a parent clicks "View personal data" 
	    				
	    		}
	    	});
	    	}
	    	return mntmStudentRemovalFrom;
	    	
	    }
	  
	  public JMenuItem getMenuItemTeacherAllocation()
	    {
	    	if(mntmTeacherAllocationTo==null)
	    	{
	    		 mntmTeacherAllocationTo = new JMenuItem("Teacher allocation to class");
	    		 mntmTeacherAllocationTo.addActionListener(new java.awt.event.ActionListener()
	    		{	
	    			public void actionPerformed(java.awt.event.ActionEvent e)
	    		
	    			{
	    				System.out.println("actionPerformed");
	    				user.dataExReplaceTeacher=null;
	    				myLoginFrame.client.handleMessageFromClientUI("ExReqTeacherRepQuery");
	    				while(user.dataExReplaceTeacher==null);
	    				
	    				
	    				apprvTeacherAllocScreen = null;
	    				apprvTeacherAllocScreen = getApprvTeacherAllocScreen();
	    				setContentPane(apprvTeacherAllocScreen);
	    				
	    				
	    				//TODO what happends when a parent clicks "View personal data" 
	    				
	    		}
	    	});
	    	}
	    	return mntmTeacherAllocationTo;
	    	
	    }
		
	  public JMenuItem getMenuItemStudentAdditionToCourse()
	    {
	    	if(mntmStudentAdditionTo==null)
	    	{
	    		 mntmStudentAdditionTo = new JMenuItem("Student addition to course");
	    		 mntmStudentAdditionTo.addActionListener(new java.awt.event.ActionListener()
	    		{	
	    			public void actionPerformed(java.awt.event.ActionEvent e)
	    		
	    			{
	    				System.out.println("actionPerformed");
	    				user.dataExAddStudToCourse=null;
	    				myLoginFrame.client.handleMessageFromClientUI("ExReqStudAddQuery");
	    				while(user.dataExAddStudToCourse==null);
	    				apprvStudentAdd=null;
	    				apprvStudentAdd = getApprvStudentAdd();
	    				setContentPane(apprvStudentAdd);
	    				
	    				//TODO what happends when a parent clicks "View personal data" 
	    				
	    		}
	    	});
	    	}
	    	return mntmStudentAdditionTo;
	    	
	    }
		
	
	
	
	
	
    public JMenu getMenuPersonalData()
    {
    	if(mPersonalData==null)
    	{
    		mPersonalData= new JMenu("Personal Data");
    	}
    	return mPersonalData;
    }
	
	
    public JMenuItem getMenuItemPersonalData()
    {
    	if(mntmViewPersonalData==null)
    	{
    		mntmViewPersonalData = new JMenuItem("View personal data");
    		mntmViewPersonalData.addActionListener(new java.awt.event.ActionListener()
    		{	
    			public void actionPerformed(java.awt.event.ActionEvent e)
    		
    			{
    				selClassScrn = null;
    				selClassScrn = getSelClassScrn();
    				setContentPane(selClassScrn);
    				
    				System.out.println("Principle view personal data");
    				
    				//TODO what happends when a parent clicks "View personal data" 
    				
    		}
    	});
    	}
    	return mntmViewPersonalData;
    	
    }
    
   
	
    public JMenu getMenuStatReports()
    {
    	if(mStatisticalReports==null)
    	{
    		 mStatisticalReports = new JMenu("Statistical Reports");
    	}
    	return mStatisticalReports;
    } 
    
    
    public JMenuItem getMenuItemStatTeacherClass()
    {
    	if(mIStatTeachersClass==null)
    	{
    		 mIStatTeachersClass = new JMenuItem("Grades between teachers for specific class");
    		 mIStatTeachersClass.addActionListener(new java.awt.event.ActionListener()
    			{	
    				public void actionPerformed(java.awt.event.ActionEvent e)
    			
    				{	user.Classes=null;
						myLoginFrame.client.handleMessageFromClientUI("ClassesQuery");
						while(user.Classes==null);
    					bTeachers4ClassScrn = null;
    					bTeachers4ClassScrn=getBTeachers4ClassScrn();
    					setContentPane(bTeachers4ClassScrn);
    					
    					
    					//set this code after selection of the class
    					System.out.println("mIStatTeachersClass");
    					
    					//TODO what happends when a parent clicks "mIStatTeachersClass" 
    					
    			}
    		});
    	}
    		
    	return mIStatTeachersClass;
    	
    }
    
    public JMenuItem getMenuItemStatCoursesForClass()
    {
    	if(mICoursesforClass==null)
    	{
    		 mICoursesforClass = new JMenuItem("Grades between diffrent courses for the same class");
			 mICoursesforClass.addActionListener(new java.awt.event.ActionListener()
				{	
					public void actionPerformed(java.awt.event.ActionEvent e)
				
					{
//						
						user.Classes=null;
    					myLoginFrame.client.handleMessageFromClientUI("ClassesQuery");
    					while(user.Classes==null);
						bCourses4ClassScrn = null;
						bCourses4ClassScrn =  getBCourses4ClassScrn();
						setContentPane(bCourses4ClassScrn);
						
						//set this code after selection of the class
						System.out.println("mIStatTeachersClass");
						
						//TODO what happends when a parent clicks "mIStatTeachersClass" 
						
				}
			});
    	}
    		
    	return mICoursesforClass;
    	
    }
    
    
    
    public JMenuItem getMenuItemStatClassesSameTeacher()
    {
    	if(mIClassesSameTeacher==null)
    	{
    		 mIClassesSameTeacher = new JMenuItem("Grades between classes for the same teacher");
			 mIClassesSameTeacher.addActionListener(new java.awt.event.ActionListener()
				{	
					public void actionPerformed(java.awt.event.ActionEvent e)
				
					{
						user.Teachers=null;
						myLoginFrame.client.handleMessageFromClientUI("TeachersForHistQuery");
						while(user.Teachers==null);
											
						
						bClasses4TeacherScrn=null;
						bClasses4TeacherScrn = getClasses4TeacherScrn();
						setContentPane(bClasses4TeacherScrn);
						
						//myLoginFrame.client.handleMessageFromClientUI("DataForHistBetweenClassesToChosenTeacher "+teacherId);
						//while(user.dataHistograms.BclassesForTeacher==null);
						//System.out.println("mIStatTeachersClass");
						// set this code after selection of the teacher - need to sent teacherId
						//TODO what happends when a parent clicks "mIStatTeachersClass" 
						
				}
			});
    	}
    		
    	return mIClassesSameTeacher;
    	
    }
    
    
    public JMenuItem getLogoutMi()
    {
    	if(logout==null)
    	{
    		 logout = new JMenuItem("Logout");
    		  logout.addActionListener(new java.awt.event.ActionListener()
    			{	
    				public void actionPerformed(java.awt.event.ActionEvent e)
    			
    				{
    					System.out.println("logout");
    					if(myLoginFrame.LoginResult.equalsIgnoreCase("OK"))
    					{
    						myLoginFrame.client.handleMessageFromClientUI("logout "+myLoginFrame.UserName.getText().toString());
    					}
    					myLoginFrame.UserName.setText("");
    					myLoginFrame.Password.setText("");
    					myLoginFrame.setContentPane(myLoginFrame.getFirstPanel());
    					myLoginFrame.setVisible(true);
    					//idTextField.setText("");
    					//userTypeTextField.setText("");
    					myLoginFrame.LoginResult=null;
    					myLoginFrame.UserType=null;
    					setVisible(false);
    					
    					
    					//TODO what happends when a parent clicks "mlogout" 
    					
    			}
    		});
    		
    	    
    	}
    		
    	return logout;
    	
    }
    
    
        
    
    
   
    
	public PrincipleMainMenu() {
		
		super();
		initialize(prmn);
		
		

	}
	
	public void setDataHistBetweenTeachersInChosenClass(Vector <BetweenTeachersChClass> c)
	{
		this.user.dataHistograms.BTeachForClass=c;
	}
	public void setDataForHistBetweenClassesToChosenTeacher(Vector <BetweenClassesChTeacher> c)
	{
		this.user.dataHistograms.BclassesForTeacher=c;
	}
	
	public void setDataForHistBetweenCoursesToChosenClass(Vector <BetweenCoursesToClass> c)
	{
		this.user.dataHistograms.BcoursesForClass=c;
	}
	public void setDesExReqAddStudToCourse(boolean b)
	{
		this.user.DecisionUpdated=b;
	}
	
	public PrincipleVpdSelClass getSelClassScrn()
	{
		if(selClassScrn == null)
		{
			selClassScrn = new PrincipleVpdSelClass(this);
			selClassScrn.setSize(getMaximumSize());
			selClassScrn.myLoginFrame=myLoginFrame;
			selClassScrn.prmn=this;
			selClassScrn.user=user;
		}
		
		return selClassScrn;
	}
	
	
	public PrincipleVpdSelStudent getSelStudentScrn()
	{
		if(selStudentScrn == null)
		{
			selStudentScrn = new PrincipleVpdSelStudent(this);
			selStudentScrn.setSize(getMaximumSize());
			selStudentScrn.myLoginFrame=myLoginFrame;
			selStudentScrn.prmn=this;
			selStudentScrn.user=user;
		}
		
		return selStudentScrn;
	}
	
	
	public PrincipleVpdSelYearnSem getSelYearNSemScrn()
	{
		if(selYearNSemScrn == null)
		{
			selYearNSemScrn = new PrincipleVpdSelYearnSem(this);
			selYearNSemScrn.setSize(getMaximumSize());
			selYearNSemScrn.myLoginFrame=myLoginFrame;
			selYearNSemScrn.prmn=this;
			selYearNSemScrn.user=user;
		}
		
		return selYearNSemScrn;
	}
	
	public PrincipleShowDataScreen getShowDataScrn()
	{
		if(showDataScrn == null)
		{
			showDataScrn = new PrincipleShowDataScreen(this);
			showDataScrn.setSize(getMaximumSize());
			showDataScrn.myLoginFrame=myLoginFrame;
			showDataScrn.prmn=this;
			showDataScrn.user=user;
		}
		
		return showDataScrn;
	}
	

	public void setExReqTeacherRepQuery(Vector<DataForExPrincipleReplaceTeacher> b)
	{
		this.user.dataExReplaceTeacher=b;
		for(int i=0;i<this.user.dataExReplaceTeacher.size();i++)
		{
			System.out.println("Exreq: "+b.get(i).Cid+" "+b.get(i).CourseName+" "+b.get(i).ClassId+" "+b.get(i).SubTid+" "+b.get(i).SubTeachName+" "+b.get(i).RepTid+" "+b.get(i).RepTeachName+" "+b.get(i).Rid);
		}
	}
	
	public void setExReqStudAddQuery(Vector<DataForExPrincipleAddStudToCorse> b)
	{
		this.user.dataExAddStudToCourse=b;
		for(int i=0;i<this.user.dataExAddStudToCourse.size();i++)
		{
			System.out.println("Exreq: "+b.get(i).Cid+" "+b.get(i).CourseName+" "+b.get(i).ClassId+" "+b.get(i).Sid+" "+b.get(i).StudName+" "+b.get(i).Rid);
		}
	}
	
	public void setExReqStudRemQuery(Vector<DataForExPrincipleRemoveStudFromCourse> b)
	{
		this.user.dataExRemStudToCourse=b;
		for(int i=0;i<this.user.dataExRemStudToCourse.size();i++)
		{
			System.out.println("Exreq: "+b.get(i).Cid+" "+b.get(i).CourseName+" "+b.get(i).Sid+" "+b.get(i).StudName+" "+b.get(i).Rid);
		}
	}
	

	
	public StatRepBClasses4Teacher getClasses4TeacherScrn()
	{
		if(bClasses4TeacherScrn==null)
		{
			bClasses4TeacherScrn = new StatRepBClasses4Teacher(this);
			bClasses4TeacherScrn.setSize(getMaximumSize());
			bClasses4TeacherScrn.myLoginFrame=myLoginFrame;
			bClasses4TeacherScrn.user=user;
			bClasses4TeacherScrn.prmn=this;
			
		}
		
		return bClasses4TeacherScrn;
	}
	
	public StatRepBCourses4Class getBCourses4ClassScrn()
	{
		if(bCourses4ClassScrn==null)
		{
			bCourses4ClassScrn = new StatRepBCourses4Class(this);
			bCourses4ClassScrn.setSize(getMaximumSize());
			bCourses4ClassScrn.myLoginFrame=myLoginFrame;
			bCourses4ClassScrn.user=user;
			bCourses4ClassScrn.prmn=this;
			
		}
		
		return bCourses4ClassScrn;
	}
	
	
	public StatRepBTeachers4Grade getBTeachers4ClassScrn()
	{
		if(bTeachers4ClassScrn==null)
		{
			bTeachers4ClassScrn = new StatRepBTeachers4Grade(this);
			bTeachers4ClassScrn.setSize(getMaximumSize());
			bTeachers4ClassScrn.myLoginFrame=myLoginFrame;
			bTeachers4ClassScrn.user=user;
			bTeachers4ClassScrn.prmn=this;
			
		}
		
		return bTeachers4ClassScrn;
	}
	
	public Histogram getHistogram(int arrSize)
	{
		if(his==null)
		{
			his = new Histogram(this,arrSize);
			//his.setArrSize(arrSize);
			his.setData(this);
			System.out.println("Created his");
			
		}
		return his;
	}
	
	
	public Histogram4StatBCourses4Class getHistogramC4C(int arrSize)
	{
		if(hisC4C==null)
		{
			hisC4C = new Histogram4StatBCourses4Class(this,arrSize);
			//his.setArrSize(arrSize);
			hisC4C.setData(this);
			System.out.println("Created his");
			
		}
		return hisC4C;
	}
	
	public Histogram4StatBTeacher4Class getHistogramT4C(int arrSize)
	{
		if(hisT4C==null)
		{
			hisT4C = new Histogram4StatBTeacher4Class(this,arrSize);
			//his.setArrSize(arrSize);
			hisT4C.setData(this);
			System.out.println("Created his");
			
		}
		return hisT4C;
	}
	
	
	public void setClassesPrinciple(Vector<String> c)
	{
		this.user.Classes=c;
	}
	public void setTeachersPrinciple(Vector<TeacherClass> t)
	{
		this.user.Teachers=t;
	}
	public void setTeachersForHisPrinciple(Vector<TeacherClass> t)
	{
		this.user.Teachers=t;
		for(int i=0;i<this.user.Teachers.size();i++)
		{
			System.out.println("\nTeacher: "+this.user.Teachers.get(i).FirstName+" "+this.user.Teachers.get(i).LastName);
		}
	}
	//to execute the decision on student add to a course exeptionaly
	// run from client "SetDesExReqAddStudToCourse "+rid+" "+aprove(true/false)
	//wait until this.user.DecisionUpdated=true
	
	//to execute the decision on student removal from course
	//run from client "SetDesExReqRemovalStudToCourse"+rid++aprove(true/false)
	//wait until this.user.DecisionUpdated=true
	
	
	//to execute the decision on student removal from course
	//run from client "SetDesExReqReplaceTeacher"+rid++aprove(true/false)
	//wait until this.user.DecisionUpdated=true


}
